package com.offcn.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import lombok.Data;
import lombok.ToString;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
@Data
@ToString
public class OssTemplate {
    private String endpoint;
    private String accessKeyId;
    private String accessKeySecret;
    private String  bucketName;
    //文件上传
    public String upload(InputStream inputStream,String fileName){
       //1.文件上传
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM--dd");
        String foldName=simpleDateFormat.format(new Date());//文件夹名字
       fileName= UUID.randomUUID().toString().replace("-","")+""+fileName;
        //
        OSS ossClient = new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);
        //3、// 上传文件流，指定bucket的名称
        ossClient.putObject(bucketName,"pic/"+foldName+"/"+fileName,inputStream);
        //guanbi
        ossClient.shutdown();
        String url="http://"+bucketName+endpoint+"/pic/"+foldName+"/"+fileName;
        return url;
    }
}
